home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Caml Light 0.61 / Source / src / runtime / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-24  |  2.7 KB  |  114 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #include "config.h"
  3. #include "debugger.h"
  4. #include "misc.h"
  5.  
  6. int verb_gc;
  7.  
  8. void gc_message (msg, arg)
  9.      char *msg;
  10.      unsigned long arg;
  11. {
  12.   if (verb_gc){
  13.     fprintf (stderr, msg, arg);
  14.     fflush (stderr);
  15.   }
  16. }
  17.  
  18. void fatal_error (s)
  19.      char *s;
  20. {
  21.   fprintf (stderr, "Fatal error: %s.\n", s);
  22.   exit(2);
  23. }
  24.  
  25. extern int errno;
  26. extern char * error_message();
  27.  
  28. void fatal_unix_error (s1, s2)
  29.      char * s1, * s2;
  30. {
  31.   fprintf (stderr, "Fatal error: %s%s: %s.\n", s1, s2, error_message (errno));
  32.   exit(2);
  33. }
  34.  
  35.  
  36. #ifdef USING_MEMMOV
  37.  
  38. /* This should work on 64-bit machines as well as 32-bit machines.
  39.    It assumes a long is the natural size for memory reads and writes.
  40. */
  41. void memmov (dst, src, length)
  42.      char *dst, *src;
  43.      unsigned long length;
  44. {
  45.   unsigned long i;
  46.  
  47.   if ((unsigned long) dst <= (unsigned long) src){
  48.     /* Copy in ascending order. */
  49.     if (((unsigned long) src - (unsigned long) dst) % sizeof (long) != 0){
  50.       /* The pointers are not equal modulo sizeof (long). Copy byte by byte. */
  51.       for (; length > 0; length--){
  52.     *dst++ = *src++;
  53.       }
  54.     }else{
  55.       /* Copy the first few bytes. */
  56.       i = (unsigned long) dst % sizeof (long);
  57.       if (i != 0){
  58.     for (; i < sizeof (long); i++){
  59.       *dst++ = *src++; --length;
  60.     }
  61.       }
  62.       /* Then copy as many entire words as possible. */
  63.       for (i = length / sizeof (long); i > 0; i--){
  64.     *(long *) dst = *(long *) src;
  65.     dst += sizeof (long); src += sizeof (long);
  66.       }
  67.       /* Then copy the last few bytes. */
  68.       for (i = length % sizeof (long); i > 0; i--){
  69.     *dst++ = *src++;
  70.       }
  71.     }
  72.   }else{
  73.     /* Copy in descending order. */
  74.     src += length; dst += length;
  75.     if (((unsigned long) dst - (unsigned long) src) % sizeof (long) != 0){
  76.       /* The pointers are not equal modulo sizeof (long). Copy byte by byte. */
  77.       for (; length > 0; length--){
  78.     *--dst = *--src;
  79.       }
  80.     }else{
  81.       /* Copy the first few bytes. */
  82.       for (i = (unsigned long) dst % sizeof (long); i > 0; i--){
  83.     *--dst = *--src; --length;
  84.       }
  85.       /* Then copy as many entire words as possible. */
  86.       for (i = length / sizeof (long); i > 0; i--){
  87.     dst -= sizeof (long); src -= sizeof (long);
  88.     *(long *) dst = *(long *) src;
  89.       }
  90.       /* Then copy the last few bytes. */
  91.       for (i = length % sizeof (long); i > 0; i--){
  92.     *--dst = *--src;
  93.       }
  94.     }
  95.   }
  96. }
  97.  
  98. #endif /* USING_MEMMOV */
  99.  
  100. char *aligned_malloc (size, modulo)
  101.      asize_t size;
  102.      int modulo;
  103. {
  104.   char *raw_mem;
  105.   unsigned long aligned_mem;
  106.  
  107.   Assert (modulo < Page_size);
  108.   raw_mem = (char *) malloc (size + Page_size);
  109.   if (raw_mem == NULL) return NULL;
  110.   raw_mem += modulo;        /* Address to be aligned */
  111.   aligned_mem = (((unsigned long) raw_mem / Page_size + 1) * Page_size);
  112.   return (char *) (aligned_mem - modulo);
  113. }
  114.